package com.leyou.trade.service.impl;

import com.leyou.trade.entity.OrderDetail;
import com.leyou.trade.service.OrderDetailService;
import com.leyou.trade.service.OrderService;
import com.leyou.trade.service.PayService;
import com.leyou.trade.utils.PayHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private PayHelper payHelper;
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    public String generatePayCode(Long orderId) {

        Long actualFee = this.orderService.getById(orderId).getActualFee();

        List<OrderDetail> orderDetails = this.orderDetailService.query().eq("order_id", orderId).list();

        String payUrl = this.payHelper.getPayUrl(orderId,1L/*actualFee*/,orderDetails.get(0).getTitle());

        //TODO payUrl 有2小时有效期，第一次肯定要web请求，之后，可以缓存url，缓存2小时
        return payUrl;
    }

    @Override
    public Integer queryPayStatus(Long id) {
        return this.orderService.getById(id).getStatus();
    }
}
